package com.hiwan.unions.activity.membership;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.adapter.bean.MembersBean;
import com.hiwan.unions.application.exception.ApiException;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.ToastUtils;
import com.hiwan.unions.util.Utils;
import com.hiwan.unions.view.popupWindow.MembershipPop;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by llg on 2017/8/11.
 */
public class MembershipUpdateActivity extends RootActivity implements MembershipPop.OnTextLinstener, HttpListener<JSONObject> {
    Request<JSONObject> request;
    private ArrayList<MembersBean> arry_wh = new ArrayList<MembersBean>();
    private ArrayList<MembersBean> arry_zz = new ArrayList<MembersBean>();
    private ArrayList<String> arry_years = new ArrayList<String>();
    private EditText edit_userAddress;
    private TextView edit_wh, edit_zz;
    private int i = 0;
    private MembershipPop membershipPop;
    private String code2, code3;

    public static void startPreview(Context context, Bundle bundle) {
        Intent intent = new Intent(context, MembershipUpdateActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_membership_update;
    }

    @Override
    protected void initData() {
        stringArrayList(Utils.getJson(this, "wenhua.txt"), arry_wh);
        stringArrayList(Utils.getJson(this, "zz.txt"), arry_zz);
        bundle = getIntent().getExtras();
        try {

            JSONObject jsonObject = new JSONObject(bundle.getString("json"));
            edit_userAddress.setText(JsonUtil.getString(jsonObject, "member_household_addr"));
            code2 = JsonUtil.getString(jsonObject, "member_culture");
            code3 = JsonUtil.getString(jsonObject, "member_politicalStatus");
            Logger.i("");
            fo2(arry_wh, code2, edit_wh);
            fo2(arry_zz, code3, edit_zz);
        } catch (JSONException j) {
            j.printStackTrace();
        }

    }

    private String fo2(ArrayList<MembersBean> list, String id, TextView textView) {
        for (int i = 0; i < list.size(); i++) {
            MembersBean bean = list.get(i);
            if (bean.getId().equals(id)) {
                textView.setText(bean.getTitle());
                return bean.getId();
            }
        }
        return "";
    }

    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        edit_userAddress = findView(R.id.edit_userAddress);
        edit_wh = findView(R.id.edit_wh);
        edit_zz = findView(R.id.edit_zz);
        setTitleTxt("更新资料");
        setOperationBtn("提交");
        membershipPop = new MembershipPop(this);
        membershipPop.setOnTextLinstener(this);
    }

    @Override
    public void getOperationBtn() {
        super.getOperationBtn();
        request = NoHttp.createJsonObjectRequest(ConstsNet.updateMember, RequestMethod.POST);
        try {
            request.add("member_culture", code2);
            request.add("member_politicalStatus", code3);
            request.add("member_household_addr", CheckUtils.check(edit_userAddress, getString(R.string.enter_household_error2)));
        } catch (ApiException e) {
            ToastUtils.showToast(this, e.getMessage());
            return;
        }
        CallServer.getRequestInstance().addToken(this, 0, request, this, true, userInfo);

    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        MembershipshenheActivity.startPreview2(this, "会籍消息");
        finish();
    }

    private void stringArrayList(String s, ArrayList<MembersBean> strings) {
        try {
            JSONArray jsonArray = new JSONArray(s);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                MembersBean bean = new MembersBean();
                bean.setId(JsonUtil.getString(jsonObject, "id"));
                bean.setTitle(JsonUtil.getString(jsonObject, "title"));
                strings.add(bean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void ButtonOnClick(View view) {
        switch (view.getId()) {
            case R.id.wh_layout:
                i = 2;
                OnClicks(view, stringArrayList(arry_wh), edit_wh.getText().toString());
                break;
            case R.id.zz_layout:
                i = 3;
                OnClicks(view, stringArrayList(arry_zz), edit_zz.getText().toString());
                break;

        }
    }

    private void OnClicks(View view, ArrayList<String> list, String content) {
        if (list.size() == 0) {
            ToastUtils.showToast(this, "暂无数据选择");
            return;
        }
        membershipPop.showPopupWindow(view, list, content);

    }

    private String stringArrayList(ArrayList<MembersBean> strings, String title) {
        for (MembersBean s : strings) {
            if (s.getTitle().equals(title)) {
                return s.getId();
            }
        }
        return "";
    }

    private ArrayList<String> stringArrayList(ArrayList<MembersBean> strings) {
        arry_years.clear();
        for (MembersBean s : strings) {
            arry_years.add(s.getTitle());
        }
        return arry_years;
    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {

    }

    @Override
    public void OnText(String text, int z) {
        if (i == 2) {
            code2 = arry_wh.get(z).getId();
            edit_wh.setText(text);
        } else if (i == 3) {
            code3 = arry_zz.get(z).getId();
            edit_zz.setText(text);
        }
    }
}